package fr.teuteuf.runstickmanrun.map.block;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.World;

import fr.teuteuf.runstickmanrun.Constants;
import fr.teuteuf.runstickmanrun.map.MapGenerator;
import fr.teuteuf.runstickmanrun.map.tile.SolidBoxTile;
import fr.teuteuf.runstickmanrun.map.tile.Tile;

public class StairsAndTowerBlock extends AbstractMapBlock {
	
	private static final int HOLE_HEIGHT = 4;
	
	public StairsAndTowerBlock(int difficultyLevel) {
		setDifficulties(0, 1, 4);
		setCurrentLevel(difficultyLevel);
	}

	@Override
	public int generateBlock(World world, MapGenerator mg, int nbHorizontalTiles) {
		boolean damagingTower;
		int nbDamage = 5;
		int blockLength = 0;
		int stairsNumber = MathUtils.random(8, 17);
		int gapSize = MathUtils.random(5, 8);
		int holePosition = MathUtils.random(1, stairsNumber + 3);
		
		if(getCurrentLevel() >= getMaxDifficultyLevel()) {
			damagingTower = true;
		} else if(getCurrentLevel() >= getMedDifficultyLevel()) {
			damagingTower = MathUtils.randomBoolean();
		} else {
			damagingTower = false;
		}
		
		for(int i = 1; i < stairsNumber; i++) {
			if(i == stairsNumber - 1) {
				mg.addTile(new SolidBoxTile((byte) 11, 7, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, i * Tile.TILE_SIZE, world));
				buildBlockColumn(0, (byte) 2, (nbHorizontalTiles + blockLength), 0, i - 1, 1, 2, world, mg);
			} else {
				mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, i * Tile.TILE_SIZE, world));
				buildBlockColumn(0, (byte) 0, (nbHorizontalTiles + blockLength), 0, i - 1, 1, 2, world, mg);
			}
			blockLength++;
		}
		
		blockLength += gapSize;
		
		int topPosition = Constants.HEIGHT_RESOLUTION / Tile.TILE_SIZE - 1;
		if(holePosition + HOLE_HEIGHT < topPosition - 2) {
			mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, topPosition * Tile.TILE_SIZE, world));
			mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength + 1) * Tile.TILE_SIZE, topPosition * Tile.TILE_SIZE, world));
			buildBlockColumn(0, (byte) 0, (nbHorizontalTiles + blockLength) + 1, topPosition - 1, holePosition + HOLE_HEIGHT + 1, 1, 2, world, mg);
			mg.addTile(new SolidBoxTile((byte) 3, 2, 1, (nbHorizontalTiles + blockLength + 2) * Tile.TILE_SIZE, topPosition * Tile.TILE_SIZE, world));
			
			if(damagingTower) {
				buildBlockColumn(nbDamage, (byte) 8, (nbHorizontalTiles + blockLength), topPosition - 1, holePosition + HOLE_HEIGHT + 2, 2, 9, world, mg);
				buildBlockColumn(nbDamage, (byte) 2, (nbHorizontalTiles + blockLength) + 2, topPosition - 1, holePosition + HOLE_HEIGHT + 2, 3, 9, world, mg);
			} else {
				buildBlockColumn(0, (byte) 8, (nbHorizontalTiles + blockLength), topPosition - 1, holePosition + HOLE_HEIGHT + 2, 1, 2, world, mg);
				buildBlockColumn(0, (byte) 2, (nbHorizontalTiles + blockLength) + 2, topPosition - 1, holePosition + HOLE_HEIGHT + 2, 1, 2, world, mg);
			}
			
			mg.addTile(new SolidBoxTile((byte) 12, 1, 2, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (holePosition + HOLE_HEIGHT + 1) * Tile.TILE_SIZE, world));
			mg.addTile(new SolidBoxTile((byte) 4,  1, 2, (nbHorizontalTiles + blockLength + 1) * Tile.TILE_SIZE, (holePosition + HOLE_HEIGHT + 1) * Tile.TILE_SIZE, world));
			mg.addTile(new SolidBoxTile((byte) 6,  1, 2, (nbHorizontalTiles + blockLength + 2) * Tile.TILE_SIZE, (holePosition + HOLE_HEIGHT + 1) * Tile.TILE_SIZE, world));
		}
		mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, holePosition * Tile.TILE_SIZE, world));
		buildBlockColumn(0, (byte) 8, (nbHorizontalTiles + blockLength), holePosition - 1, 0, 1, 2, world, mg);
		mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength + 1) * Tile.TILE_SIZE, holePosition * Tile.TILE_SIZE, world));
		buildBlockColumn(0, (byte) 0, (nbHorizontalTiles + blockLength) + 1, holePosition - 1, 0, 1, 2, world, mg);
		mg.addTile(new SolidBoxTile((byte) 3, 2, 1, (nbHorizontalTiles + blockLength + 2) * Tile.TILE_SIZE, holePosition * Tile.TILE_SIZE, world));
		buildBlockColumn(0, (byte) 2, (nbHorizontalTiles + blockLength) + 2, holePosition - 1, 0, 1, 2, world, mg);
		blockLength += 3;
		
		
		
		return nbHorizontalTiles + blockLength;
	}
}
